using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ICSharpCode.TextEditor.Formatting
{
    public class CssFormatStrategy : IFormattingStrategy
    {
        private string ConvertCssString(string strcss)
        {
            string text = string.Empty;
            if (string.IsNullOrEmpty(strcss))
            {
                return strcss;
            }
            int num = 0;
            strcss = Regex.Replace(strcss, " {1,}", "\n", RegexOptions.IgnoreCase).Replace('\r', ' ').Replace('\n', ' ')
                .Replace('\t', ' ');
            char[] array = strcss.ToCharArray();
            List<char> list = new List<char>();
            int i = 0;
            for (int num2 = array.Length; i < num2; i++)
            {
                char c = array[i];
                if (c != ' ')
                {
                    if (c == '{')
                    {
                        list.Add(c);
                        list.Add('\n');
                    }
                    if (c == '}')
                    {
                        int num3 = i;
                        char c2 = ' ';
                        while (num3 > 0)
                        {
                            num3--;
                            char c3 = array[num3];
                            if (c3 != ' ')
                            {
                                num3 = 0;
                                c2 = c3;
                                break;
                            }
                        }
                        if (c2 != ';')
                        {
                            list.Add('\n');
                        }
                        list.Add(c);
                        list.Add('\n');
                    }
                    if (c == ';')
                    {
                        list.Add(c);
                        list.Add('\n');
                    }
                    if (c == '*')
                    {
                        if (array[i - 1] != '/')
                        {
                            list.Add('\n');
                            list.Add(c);
                        }
                        else
                        {
                            list.Add(c);
                        }
                    }
                    if (c == '/')
                    {
                        if (i > 1 && array[i - 1] == '*')
                        {
                            list.Add(c);
                            list.Add('\n');
                        }
                        else
                        {
                            list.Add(c);
                        }
                    }
                    if (c != '{' && c != '}' && c != ';' && c != '*' && c != '/')
                    {
                        list.Add(c);
                    }
                }
                else if (i > 0 && i < num2 - 1 && array[i - 1] != ' ' && array[i + 1] != ' ')
                {
                    list.Add(c);
                }
            }
            string text2 = string.Concat(list.ToArray());
            string[] array2 = text2.Split('\n');
            string[] array3 = array2;
            foreach (string text3 in array3)
            {
                if (!(text3 != ""))
                {
                    continue;
                }
                string text4 = text3.ToString().Trim();
                if (text4 == "{" || text4.EndsWith("{"))
                {
                    for (int k = 0; k < num; k++)
                    {
                        text4 = "       " + text4;
                    }
                    num++;
                }
                if (!text4.Contains("{") && text4 != "}")
                {
                    for (int l = 0; l < num; l++)
                    {
                        text4 = "       " + text4;
                    }
                }
                if (text4 == "}")
                {
                    num--;
                    for (int m = 0; m < num; m++)
                    {
                        text4 = "       " + text4;
                    }
                }
                text = text + text4 + "\r\n";
            }
            text2 = null;
            list.Clear();
            list = null;
            return text;
        }

        private string ConvertCssString(string strcss, int indent)
        {
            string text = string.Empty;
            if (string.IsNullOrEmpty(strcss))
            {
                return strcss;
            }
            int num = indent;
            strcss = Regex.Replace(strcss, " {1,}", "\n", RegexOptions.IgnoreCase).Replace('\r', ' ').Replace('\n', ' ')
                .Replace('\t', ' ');
            char[] array = strcss.ToCharArray();
            List<char> list = new List<char>();
            int i = 0;
            for (int num2 = array.Length; i < num2; i++)
            {
                char c = array[i];
                if (c != ' ')
                {
                    if (c == '{')
                    {
                        list.Add(c);
                        list.Add('\n');
                    }
                    if (c == '}')
                    {
                        int num3 = i;
                        char c2 = ' ';
                        while (num3 > 0)
                        {
                            num3--;
                            char c3 = array[num3];
                            if (c3 != ' ')
                            {
                                num3 = 0;
                                c2 = c3;
                                break;
                            }
                        }
                        if (c2 != ';')
                        {
                            list.Add('\n');
                        }
                        list.Add(c);
                        list.Add('\n');
                    }
                    if (c == ';')
                    {
                        list.Add(c);
                        list.Add('\n');
                    }
                    if (c == '*')
                    {
                        if (array[i - 1] != '/')
                        {
                            list.Add('\n');
                            list.Add(c);
                        }
                        else
                        {
                            list.Add(c);
                        }
                    }
                    if (c == '/')
                    {
                        if (i > 1 && array[i - 1] == '*')
                        {
                            list.Add(c);
                            list.Add('\n');
                        }
                        else
                        {
                            list.Add(c);
                        }
                    }
                    if (c != '{' && c != '}' && c != ';' && c != '*' && c != '/')
                    {
                        list.Add(c);
                    }
                }
                else if (i > 0 && i < num2 - 1 && array[i - 1] != ' ' && array[i + 1] != ' ')
                {
                    list.Add(c);
                }
            }
            string text2 = string.Concat(list.ToArray());
            string[] array2 = text2.Split('\n');
            string[] array3 = array2;
            foreach (string text3 in array3)
            {
                if (!(text3 != ""))
                {
                    continue;
                }
                string text4 = text3.ToString().Trim();
                if (text4 == "{" || text4.EndsWith("{"))
                {
                    for (int k = 0; k < num; k++)
                    {
                        text4 = "       " + text4;
                    }
                    num++;
                }
                if (!text4.Contains("{") && text4 != "}")
                {
                    for (int l = 0; l < num; l++)
                    {
                        text4 = "       " + text4;
                    }
                }
                if (text4 == "}")
                {
                    num--;
                    for (int m = 0; m < num; m++)
                    {
                        text4 = "       " + text4;
                    }
                }
                text = text + text4 + "\r\n";
            }
            text2 = null;
            list.Clear();
            list = null;
            return text;
        }

        public string StringFormat(string content)
        {
            string empty = string.Empty;
            try
            {
                return ConvertCssString(content);
            }
            catch (Exception)
            {
                return content;
            }
        }

        public string StringFormat(string content, int Indent)
        {
            string empty = string.Empty;
            try
            {
                return ConvertCssString(content, Indent);
            }
            catch (Exception)
            {
                return content;
            }
        }
    }
}
